package visualisation;

import prefuse.util.force.AbstractForce;
import prefuse.util.force.Spring;

public class SpringGravForce extends AbstractForce{

	private static String[] pnames 
    = new String[] { "SpringGravCoeff","SpringFravDirection" };

	
    public static final int GRAV_CONST = 0;
    public static final int GRAV_DIRECTION = 1;	
	
    public static final float DEFAULT_SPRING_GRAV_CONST = 1E-4f;
    public static final float DEFAULT_MAX_SPRING_GRAV_CONST = 1E-3f;
    public static final float DEFAULT_MIN_SPRING_GRAV_CONST = 1E-5f;

    public static final float DEFAULT_SPRING_GRAV_DIRECTION = (float)Math.PI/2;
    public static final float DEFAULT_MAX_SPRING_GRAV_DIRECTION = (float)-Math.PI;
    public static final float DEFAULT_MIN_SPRING_GRAV_DIRECTION = (float)Math.PI;
	
	public SpringGravForce(float forceConstant, float direction) {
        params = new float[] { forceConstant, direction };
        minValues = new float[] 
            { DEFAULT_MIN_SPRING_GRAV_CONST, DEFAULT_MIN_SPRING_GRAV_DIRECTION };
        maxValues = new float[] 
            {DEFAULT_MAX_SPRING_GRAV_CONST,DEFAULT_MAX_SPRING_GRAV_DIRECTION};
	}
	
    public SpringGravForce() {
        this(DEFAULT_SPRING_GRAV_CONST, DEFAULT_SPRING_GRAV_DIRECTION);
    }
    
    
    /**
     * Returns true.
     * @see prefuse.util.force.Force#isSpringForce()
     */
    public boolean isSpringForce() {
        return true;
    }
    
    /**
     * @see prefuse.util.force.AbstractForce#getParameterNames()
     */
    protected String[] getParameterNames() {
        return pnames;
    }
    
    public void getForce(Spring s) {   	
    	float theta = params[GRAV_DIRECTION];
    	float coeff1 = params[GRAV_CONST]*s.item1.mass;
        float coeff2 = params[GRAV_CONST]*s.item2.mass;
        
        s.item1.force[0] -= Math.cos(theta)*coeff1;
        s.item1.force[1] -= Math.sin(theta)*coeff1;

        
        s.item2.force[0] += Math.cos(theta)*coeff2;
        s.item2.force[1] += Math.sin(theta)*coeff2;
    }
         

}
